package com.tommy.common.util;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Package Name: com.tommy.common.util
 * @Description: TODO
 * @Author tommy
 * @Date 2017.11.21 10:27
 * @Modify Who      When     What
 * ---------- ----------- -------------
 */
public class MybatisGeneratorCommon implements CommentGenerator {

    @Override
    public void addConfigurationProperties(Properties properties) {
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (StringUtils.isEmpty(introspectedColumn.getRemarks()))
            return;

        field.addJavaDocLine("/**" +
                "\n" +
                introspectedColumn.getActualColumnName() +
                ": " +
                introspectedColumn.getRemarks()+
                "\n" +
                "*/");
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        StringBuilder sb = new StringBuilder();
        topLevelClass.addJavaDocLine("/**"+" * 描述:" + introspectedTable.getFullyQualifiedTable()+"表的实体类"+" */");
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {
    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        StringBuilder sb = new StringBuilder();
        method.addJavaDocLine("/**");
        sb.append(" * 描述:");
        // 方法名
        if (method.getName().contains("insert")) {
            sb.append(" 插入数据");
        } else if (method.getName().contains("delete")) {
            sb.append(" 删除数据");
        } else if (method.getName().contains("update")) {
            sb.append(" 更新数据");
        } else if (method.getName().contains("select")) {
            sb.append(" 查询数据");
        }
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        sb.setLength(0);
        sb.append(" * ");
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        // 参数
        if (method.getParameters().size() != 0) {
            for (Parameter ignored : method.getParameters()) {
                sb.setLength(0);
                sb.append(" * @param ");
                method.addJavaDocLine(sb.toString().replace("\n", " "));
            }
        }
        sb.setLength(0);
        // 返回值
        if (method.getReturnType() != null) {
            sb.append(" * @return");
            method.addJavaDocLine(sb.toString().replace("\n", " "));
        }
        method.addJavaDocLine(" */");
    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {

    }

    @Override
    public void addComment(XmlElement xmlElement) {

    }

    @Override
    public void addRootComment(XmlElement xmlElement) {

    }

    /**
     * 手动调用myBatisGenerator来生成 mapper/dao/entity
     *
     * @param generatorConfigPath 配置文件路径
     */
    public static void run(String generatorConfigPath) {
        try {
            List<String> warnings = new ArrayList<>();
            boolean overwrite = true;
            File configFile = new File(generatorConfigPath);
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (SQLException | IOException | InterruptedException | InvalidConfigurationException | XMLParserException e) {
            e.printStackTrace();
        }
    }
}
